<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-card" shadow="hover">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
        <el-form-item label="用户名" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入用户名"
            clearable
            prefix-icon="el-icon-user"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="答对数量" prop="correctCount">
          <el-input
            v-model="queryParams.correctCount"
            placeholder="请输入答对题目数量"
            clearable
            prefix-icon="el-icon-trophy"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="isDeleted">
          <el-select v-model="queryParams.isDeleted" placeholder="请选择状态" clearable style="width: 120px">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作区域 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">排行榜列表</span>
        <div class="action-buttons">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['question:ranking:export']"
          >导出</el-button>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>
      </div>

      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="rankingList"
        @selection-change="handleSelectionChange"
        border
        stripe
        highlight-current-row
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="排名" align="center" width="80">
          <template slot-scope="scope">
            <el-tag :type="getRankTagType(scope.$index)" effect="plain">
              {{ scope.$index + 1 }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="用户名" align="center" min-width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.sysUser ? scope.row.sysUser.nickName : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="答对数量" align="center" prop="correctCount" min-width="120">
          <template slot-scope="scope">
            <span class="correct-count">{{ scope.row.correctCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="isDeleted" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.isDeleted"/>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改排行榜对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!-- 对话框内容保持不变 -->
    </el-dialog>
  </div>
</template>

<script>
import { listRanking, getRanking, delRanking, addRanking, updateRanking } from "@/api/question/ranking";

export default {
  name: "Ranking",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排行榜数据
      rankingList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        correctCount: null,
        isDeleted: '0'
      },
      showDeleted: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 获取排名标签类型 */
    getRankTagType(index) {
      switch(index) {
        case 0: return 'danger';  // 第一名
        case 1: return 'warning';  // 第二名
        case 2: return 'success';  // 第三名
        default: return 'info';  // 其他名次
      }
    },
    /** 查询排行榜管理列表 */
    getList() {
      this.loading = true;
      listRanking(this.queryParams).then(response => {
        this.rankingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        correctCount: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.isDeleted = this.showDeleted ? '1' : '0';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排行榜管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRanking(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排行榜管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRanking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRanking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排行榜管理编号为"' + ids + '"的数据项？').then(function() {
        return delRanking(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('question/ranking/export', {
        ...this.queryParams
      }, `ranking_${new Date().getTime()}.xlsx`)
    },
    toggleRecycleBin() {
      this.showDeleted = !this.showDeleted;
      this.queryParams.isDeleted = this.showDeleted ? '1' : '0';
      this.handleQuery();
    }
  }
};
</script>

<style scoped>
.search-card {
  margin-bottom: 16px;
  border-radius: 4px;
}

.box-card {
  margin-bottom: 16px;
  border-radius: 4px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #1f2f3d;
}

.action-buttons {
  float: right;
}

.action-buttons .el-button {
  margin-left: 8px;
}

.action-buttons .el-button:first-child {
  margin-left: 0;
}

.correct-count {
  font-size: 16px;
  font-weight: 600;
  color: #409EFF;
}

.el-card {
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08) !important;
}

.el-card :deep(.el-card__header) {
  padding: 12px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #fafafa;
}

.el-table {
  margin: 8px 0;
}

.el-table :deep(th) {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: 600;
}

.el-table :deep(td) {
  padding: 8px 0;
}

/* 表格hover效果 */
.el-table :deep(tbody tr:hover) td {
  background-color: #ecf5ff !important;
}

/* 输入框图标颜色 */
.el-input :deep(.el-input__icon) {
  color: #909399;
}

/* 表单项间距 */
.el-form-item {
  margin-bottom: 16px;
}
</style>
